const { execSh } = require('../execsh')
const chalk = require('chalk')
const ora = require('ora')
const { operateTemplatesFiles } = require('../file')
const inquirer = require('inquirer')
const path = require('path')
const { template1Questions, template4Questions, template6Questions } = require('../config')
const { editT1PageConfig, editT4PageConfig, editT6PageConfig } = require('../file/editConfig')

async function createTemplate(templatePath, projectPath, templateId) {
    let answer = {};
    if (templateId == 1 || templateId == 2 || templateId == 3 || templateId == 5 || templateId == 7) { // 动态生成
        answer = await template1Questions()
    }
    if (templateId == 4) {
        answer = await template4Questions()
    }
    if (templateId == 6) {
        answer = await template6Questions();
    }
    const spinner = ora(chalk.green('正在进行模板路径确认\n'))
    spinner.start() // 展示读取状态 
    const fileList = operateTemplatesFiles(templatePath, projectPath)
    if (fileList.length > 0) {
        console.log(chalk.blue('正在生成选择模板工程...'))
        fileList.forEach((name) => console.log(chalk.blue('create'), chalk.green(`${name}`)))
        spinner.succeed(chalk.green('模板生成成功')) // 调用succed 表示成功调用
            // 判断是否需要定制化pageConfig
        if (templateId == 1 || templateId == 2 || templateId == 3 || templateId == 5 || templateId == 7) {
            console.log(chalk.blue('执行动态文件配置...'))
            await editT1PageConfig(answer, projectPath)
        } else if (templateId == 4) {
            console.log(chalk.blue('执行动态文件配置...'))
            await editT4PageConfig(answer, projectPath)
        } else if (templateId == 6) {
            console.log(chalk.blue('执行动态文件配置...'))
            await editT6PageConfig(answer, projectPath)
        } else {
            process.exit(0)
        }
    } else {
        spinner.fail(chalk.red('路径确认失败')) // 调用fail展示失败
    }
}
async function pointingOrder(versions, projectPath) {
    const templates = require('../custom/index.json');
    if (Array.isArray(versions) && versions[2]) {
        // 获取目前已存中有command的数据
        let vers = templates.filter((item) => (item && !!item.command));
        let isOrderList = vers.filter((item) => {
                if (item.command === versions[2]) {
                    return true
                } else {
                    return false
                }
            })
            // 判断只匹配成功一个快捷指令
        if (Array.isArray(isOrderList) && isOrderList.length === 1) {
            let orders = isOrderList[0]
            const { id, path: pathUrl } = orders
            if (id.length < 5) {
                await createTemplate(path.resolve(path.join(__dirname, '../') + pathUrl), projectPath, id)
            } else {
                await createTemplate(path.resolve(pathUrl), projectPath, id)
            }
            return true
        } else if (Array.isArray(isOrderList) && isOrderList.length > 0) {
            let str = isOrderList.reduce((item, newItem) => {
                return item.name + ',' + newItem.name
            })
            console.log(chalk.red(`当前快捷指令存在多个匹配项:${str}`))
                // 直接结束执行 false可以为继续默认执行
            return false
        } else {
            console.log(chalk.red('未查询到对应快捷指令,进行默认执行'))
            return false
        }
    } else {
        return false
    }
}
module.exports = {
    createTemplate,
    pointingOrder
}